package com.demomaster.quickcompat.widget.base;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;

import com.demomaster.quickcompat.widget.base.QuickViewAttribute;


/**
 * 按钮通知红色点
 */
public class QuickBadgeView {

    public static void drawPoint(View view, Canvas canvas, Builder builder) {
        if (!TextUtils.isEmpty(builder.text)) {
            if(builder.paint==null) {
                builder.paint = new Paint();
            }
            builder.paint.setTextSize(builder.textSize);
            // 文字宽
            builder.textWidth = builder.paint.measureText(builder.text);
            // 文字baseline在y轴方向的位置
            builder.baseLineY = Math.abs(builder.paint.ascent() + builder.paint.descent()) / 2;
            // 文字baseline在y轴方向的位置
            float descent = Math.abs(builder.paint.descent())/2;

            builder.width = (int) (builder.paddingLeft + builder.paddingRight + builder.textWidth);
            builder.height = (int) (builder.paddingTop + builder.paddingBottom + builder.baseLineY + descent);

            builder.radius = builder.height / 2;
            int left = 0, top = 0;
            if (builder.gravity == 0) {//leftTop
                left = 0;
                top = 0;
            } else if (builder.gravity == 1) {//leftBottom
                left = 0;
                top = view.getMeasuredHeight();
            } else if (builder.gravity == 2) {//rightTop
                left = view.getMeasuredWidth();
                top = 0;
            } else if (builder.gravity == 3) {//rightBottom
                left = view.getMeasuredWidth();
                top = view.getMeasuredHeight();
            }

            builder.point = new Point((int) (left + builder.marginLeft - builder.marginRight), (int) (top + builder.marginTop - builder.marginBottom));
            int rect_left = builder.point.x - builder.width / 2;
            int rect_top = builder.point.y - builder.height / 2;
            int rect_right = rect_left+builder.width;
            int rect_bootom = rect_top+ builder.height;
            RectF rect = new RectF(rect_left,rect_top, rect_right, rect_bootom);
            if(builder.background!=null) {
                if(builder.background instanceof ColorDrawable){
                    ColorDrawable drawable = (ColorDrawable) builder.background;
                    drawable.setBounds(rect_left, rect_top, rect_right, rect_bootom);
                }
                Path path = new Path();
                path.addRoundRect(rect, builder.radius, builder.radius, Path.Direction.CW);
                canvas.clipPath(path);
                builder.background.draw(canvas);
            }else {
                canvas.drawRoundRect(rect, builder.radius, builder.radius, builder.paint);
            }
            builder.paint.setColor(builder.textColor);
            builder.paint.setTextSize(builder.textSize);
            canvas.drawText(builder.text, builder.point.x - builder.textWidth / 2, builder.point.y + builder.baseLineY, builder.paint);
        }
    }

    public static class Builder{
        float textWidth;
        float baseLineY;
        int width;
        int height;
        private Point point;
        private Paint paint;
        private int textColor = Color.WHITE;
        private float radius = 0;
        private int gravity = 0;

        private String text = "";
        private float marginLeft, marginRight, marginTop, marginBottom;
        private int textSize = 20;
        private int paddingLeft, paddingRight, paddingTop, paddingBottom;
        private Drawable background;

        public Builder() {
            if(background==null){
                background = new ColorDrawable(Color.RED);
            }
        }

        public Builder setTextSize(int textSize) {
            this.textSize = textSize;
            return this;
        }

        public Builder setPaint(Paint paint) {
            this.paint = paint;
            return this;
        }

        public Builder setText(String text) {
            this.text = text;
            return this;
        }

        public Builder setGravity(int gravity) {
            this.gravity = gravity;
            return this;
        }

        public Builder setMargin(float margin) {
            this.marginLeft = margin;
            this.marginTop = margin;
            this.marginRight = margin;
            this.marginBottom = margin;
            return this;
        }
        public Builder setMargin(float marginLeft,float marginTop,float marginRight,float marginBottom) {
            this.marginLeft = marginLeft;
            this.marginTop = marginTop;
            this.marginRight = marginRight;
            this.marginBottom = marginBottom;
            return this;
        }

        public Builder setPadding(int padding) {
            this.paddingLeft = padding;
            this.paddingTop = padding;
            this.paddingRight = padding;
            this.paddingBottom = padding;
            return this;
        }
        public Builder setPadding(int paddingLeft,int paddingTop,int paddingRight,int paddingBottom) {
            this.paddingLeft = paddingLeft;
            this.paddingTop = paddingTop;
            this.paddingRight = paddingRight;
            this.paddingBottom = paddingBottom;
            return this;
        }

        public void setBackground(Drawable background) {
            this.background = background;
        }

        public void setTextColor(int textColor) {
            this.textColor = textColor;
        }

        public void setRadius(float radius) {
            this.radius = radius;
        }

        public void setAttribute(QuickViewAttribute quickViewAttribute) {
            setText("" + quickViewAttribute.badgeText);
            setTextSize(quickViewAttribute.badgeTextSize);
            setGravity(quickViewAttribute.badgeGravity);
            setPadding(quickViewAttribute.badgePadding);
            setMargin(quickViewAttribute.badgeMargin);
            setBackground(quickViewAttribute.badgeBackground);
            setTextColor(quickViewAttribute.badgeTextColor);
            setRadius(quickViewAttribute.radius);
        }
    }
}
